package com.shelpe.services.sinbad.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.dao.KeywordsLogMapper;
import com.shelpe.services.sinbad.model.KeywordsLog;
import com.shelpe.services.sinbad.model.KeywordsLogExample;
import com.shelpe.services.sinbad.service.KeywordsLogService;

@Service
public class KeywordsLogServiceImpl implements KeywordsLogService{

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	@Autowired
	KeywordsLogMapper keywordsLogMapper;
	
	@Override
	public Integer save(KeywordsLog record) {
		return keywordsLogMapper.insertSelective(record);
	}

	@Override
	public boolean selectSemiautoExecByToday(Long keywordId) throws ParseException {
		boolean ret = false;
		KeywordsLogExample example = new KeywordsLogExample();
		example.createCriteria().andKeywordIdEqualTo(keywordId)
		.andActionEqualTo((byte)4)
		.andUpdatedTimeGreaterThanOrEqualTo(sdf.parse(sdf.format(new Date())));
		List<KeywordsLog> logs = keywordsLogMapper.selectByExample(example);
		if(logs!=null){
			if(logs.size()>0){
				ret = true;
			}
		}
		return ret;
	}

	@Override
	public KeywordsLog getOne(Long keywordId) {
		KeywordsLog log = new KeywordsLog();
		KeywordsLogExample example = new KeywordsLogExample();
		example.createCriteria().andKeywordIdEqualTo(keywordId);
		example.setOrderByClause("id desc");
		List<KeywordsLog> logs = keywordsLogMapper.selectByExample(example);
		if(logs!=null){
			if(logs.size()>0){
				log = logs.get(0);
			}
		}
		return log;
	}

}
